//
//  UniversalDesiger.js
//  SugLisSystem
//
//  Created by Mr.Right on 2013-05-08.
//  Copyright 2013 Mr.Right. All rights reserved.
//

var LABEL_LEFT = 20;
var FONT_SIZE = 16;

function UniversalDesiger(markObject, standByFlag3, type, callBack) {
	this.markObject = markObject;
	this.standByFlag3 = standByFlag3;
	this.type = type;
	this.dataId = Ti.Platform.createUUID();
	this.age = '';
	this.degree = '';
	this.value = '';
	this.callBack = callBack;
}

UniversalDesiger.prototype.create = function() {
	var self = this;
	var win = Ti.UI.createWindow({
		width : '50%',
		height : 250,
		borderRadius : 5,
		borderWidth : 1,
		borderColor : '#000',
		backgroundColor : '#fff'
	});

	var button_Cancel = Titanium.UI.createButton({
		systemButton : Titanium.UI.iPhone.SystemButton.CANCEL
	});
	button_Cancel.addEventListener('click', function(e) {
		win.close();
	})
	var button_Done = Titanium.UI.createButton({
		systemButton : Titanium.UI.iPhone.SystemButton.DONE
	});
	button_Done.addEventListener('click', function(e) {
		var errorStr = '';
		if (self.age == '') {
			errorStr += (self.markObject['age'] + '不能为空!\r\n');
		}
		if (self.degree == '') {
			errorStr += (self.markObject['degree'] + '不能为空!\r\n');
		}
		if (self.value == '') {
			errorStr += (self.markObject['value'] + '不能为空!\r\n');
		}
		if (errorStr != '') {
			//TODO 修改Alert
			alert(errorStr);
			return;
		}
		var InvestRiskDataDB = new dB('InvestRiskData');
		InvestRiskDataDB['dataid'] = self.dataId;
		InvestRiskDataDB['signno'] = self.standByFlag3;
		InvestRiskDataDB['type'] = self.type;
		InvestRiskDataDB['age'] = self.age;
		InvestRiskDataDB['degree'] = self.degree;
		InvestRiskDataDB['value'] = self.value;
		InvestRiskDataDB.insert();
		//TODO 判断是否可以添加
		win.close();
		self.callBack(self.dataId);
	});

	flexSpace = Titanium.UI.createButton({
		systemButton : Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});

	var toolbar = Titanium.UI.iOS.createToolbar({
		items : [button_Cancel, flexSpace, button_Done],
		top : 0,
		borderTop : false,
		borderBottom : true
	});
	win.add(toolbar);

	var Label_Age = Ti.UI.createLabel({
		text : this.markObject['age'],
		left : LABEL_LEFT,
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		top : 80
	});
	win.add(Label_Age);

	var Text_Age = Ti.UI.createTextField({
		hintText : '请输入' + this.markObject['age'],
		textAlign : 'right',
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		right : LABEL_LEFT,
		top : 80,
		width : '50%',
		borderStyle : Ti.UI.INPUT_BORDERSTYLE_NONE,
		returnKeyType : Titanium.UI.RETURNKEY_DEFAULT,
		keyboardType : Titanium.UI.KEYBOARD_NUMBER_PAD
	});

	Text_Age.addEventListener('change', function(e) {
		var val = e.source.value;
		val = val.replace(/[^0-9]/gi, '')
		if (val.indexOf('0') == 0) {
			val = val.substring(1);
		}
		e.source.value = val.slice(0, 10);
		self.age = e.source.value;
	});
	win.add(Text_Age);

	var Label_Degree = Ti.UI.createLabel({
		text : this.markObject['degree'],
		left : LABEL_LEFT,
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		top : 130
	});
	win.add(Label_Degree);

	var Text_Degree = Ti.UI.createTextField({
		hintText : '请输入' + this.markObject['degree'],
		textAlign : 'right',
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		right : LABEL_LEFT,
		width : '50%',
		top : 130,
		borderStyle : Ti.UI.INPUT_BORDERSTYLE_NONE,
		returnKeyType : Titanium.UI.RETURNKEY_DEFAULT,
		keyboardType : Titanium.UI.KEYBOARD_NUMBER_PAD
	});

	Text_Degree.addEventListener('change', function(e) {
		var val = e.source.value;
		val = val.replace(/[^0-9]/gi, '')
		if (val.indexOf('0') == 0) {
			val = val.substring(1);
		}
		e.source.value = val.slice(0, 10);
		self.degree = e.source.value;
	});
	win.add(Text_Degree);

	var Label_Value = Ti.UI.createLabel({
		text : this.markObject['value'],
		left : LABEL_LEFT,
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		top : 180
	});
	win.add(Label_Value);

	var Text_Value = Ti.UI.createTextField({
		hintText : '请输入' + this.markObject['value'],
		textAlign : 'right',
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		right : LABEL_LEFT,
		width : '50%',
		top : 180,
		borderStyle : Ti.UI.INPUT_BORDERSTYLE_NONE,
		returnKeyType : Titanium.UI.RETURNKEY_DEFAULT,
		keyboardType : Titanium.UI.KEYBOARD_NUMBER_PAD
	});

	Text_Value.addEventListener('change', function(e) {
		var val = e.source.value;
		val = val.replace(/[^0-9]/gi, '')
		if (val.indexOf('0') == 0) {
			val = val.substring(1);
		}
		e.source.value = val.slice(0, 10);
		self.value = e.source.value;
	});
	win.add(Text_Value);

	this.win = win;
}

UniversalDesiger.prototype.open = function() {
	this.win.open();
}

module.exports = UniversalDesiger;
